package com.ruoyi.web.controller.purchase;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.pojo.PurchaseApply;
import com.ruoyi.purchase.service.PurchaseApplyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购申请Controller
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/purchase/apply")
public class PurchaseApplyController extends BaseController
{
    @Autowired
    private PurchaseApplyService purchaseApplyService;

    /**
     * 查询采购申请列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(PurchaseApply purchaseApply)
    {
        startPage();
        List<PurchaseApply> list = purchaseApplyService.selectPurchaseApplyList(purchaseApply);
        return getDataTable(list);
    }
    /**
     * 查询采购申请列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:list')")
    @GetMapping("/applyList")
    public TableDataInfo applyList(PurchaseApply purchaseApply)
    {
        startPage();
        List<PurchaseApply> applyList = purchaseApplyService.selectApplyAll(purchaseApply);
        return getDataTable(applyList);
    }

    /**
     * 导出采购申请列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:export')")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseApply purchaseApply)
    {
        List<PurchaseApply> list = purchaseApplyService.selectPurchaseApplyList(purchaseApply);
        ExcelUtil<PurchaseApply> util = new ExcelUtil<PurchaseApply>(PurchaseApply.class);
        util.exportExcel(response, list, "采购申请数据");
    }

    /**
     * 获取采购申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(purchaseApplyService.selectPurchaseApplyById(id));
    }

    /**
     * 新增采购申请
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:add')")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseApply purchaseApply)
    {
        purchaseApply.setCreateBy(getUserId()+"");
        purchaseApplyService.insertPurchaseApply(purchaseApply);
        return success(purchaseApply);
    }

    /**
     * 修改采购申请
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:edit')")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PurchaseApply purchaseApply)
    {
        purchaseApply.setUpdateBy(getUserId()+"");
        return toAjax(purchaseApplyService.updatePurchaseApply(purchaseApply));
    }

    /**
     * 状态修改
     * @param purchaseApply
     * @return
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:edit')")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody PurchaseApply purchaseApply){
        purchaseApply.setUpdateBy(getUserId()+"");
        return toAjax(purchaseApplyService.updateStatus(purchaseApply));
    }

    /**
     * 删除采购申请
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:remove')")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(purchaseApplyService.deletePurchaseApplyByIds(ids));
    }


    @Log(title = "最后一个id值")
    @GetMapping("lastId")
    public AjaxResult selectLastId(){
        return success(purchaseApplyService.selectLastId());
    }

    /**
     * 获取采购申请编号
     * @return
     */
    @Log(title = "获取采购申请编号")
    @GetMapping("getCGSQ")
    public AjaxResult getCGSQ(){
        return success(SeqNumberUtils.getCGSQNumber());
    }
}
